package com.rao.controller;


import com.rao.config.websecurity.permitcollect.PermitAnyOne;
import com.rao.core.startcollect.StartPrintUrlTag;
import com.rao.entity.CourseSelection;
import com.rao.entity.VO.CommonResponseVO;
import com.rao.service.ICourseSelectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 选课表 前端控制器
 * </p>
 *
 * @author rao3030
 * @since 2021-11-04
 */
@Api(value = "学生抢课接口", description = "学生抢课")
@RestController
@RequestMapping("/course-selection")
public class CourseSelectionController {

    @Autowired
    private ICourseSelectionService iCourseSelectionService;

    @PermitAnyOne
    @ApiOperation("使用数据库实现写热点分散,一个线程最多4次检索数据库,数据库会出现死锁")
    @StartPrintUrlTag
    @RequestMapping(path = "/killCourseByDB/{id}", method = RequestMethod.POST)
    public CommonResponseVO killCourseByDB(@PathVariable Integer id) {
        int isOk = iCourseSelectionService.grepLessByDB(id);
        if (isOk == 1) {
            return CommonResponseVO.success("---抢课成功---");
        } else {
            return CommonResponseVO.success("---抢课失败---");
        }
    }

    @Deprecated
    @ApiOperation("使用redis实现写热点分散,扣减成功获取一个ack的token")
    @StartPrintUrlTag
    @RequestMapping(path = "/killCourseByRedis/{id}", method = RequestMethod.POST)
    public CommonResponseVO killCourseByRedis(@PathVariable Integer id) {

        boolean isOk = iCourseSelectionService.grepLessByRedisSegment(id);
        if (isOk) {
            return CommonResponseVO.success("---抢课成功获取一个token---");
        } else {
            return CommonResponseVO.success("---抢课失败---");
        }
    }

    @ApiOperation("查询单个选课by-id")
    @StartPrintUrlTag
    @RequestMapping(path = "/courseSelectionById/{id}", method = RequestMethod.POST)
    public CommonResponseVO courseSelectionById(@PathVariable Integer id) {
        //目标先先显示sum
        CourseSelection byId = iCourseSelectionService.courseSelectionById(id);
        if (byId != null) {
            return CommonResponseVO.success(CommonResponseVO.SUCCESS_STATUS, "按你需要查询的结果", byId);
        } else {
            return CommonResponseVO.success(CommonResponseVO.SUCCESS_STATUS, "不好意思，没查到");
        }
    }


}
